package cn.com.api.controller;

import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.com.api.entity.ApiU_discover_spec;

import cn.com.api.service.ApiU_discover_specService;

import cn.com.util.*;

@Controller
@RequestMapping(Utils.SYS_API + "u_discover_spec/")
public class ApiU_discover_specController {
    @Autowired
    private ApiU_discover_specService apiU_discover_specService;
	
	private static Logger logger = Logger.getLogger(ApiU_discover_specController.class);
	
	/**
	 * 鏂板
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value = "insertU_discover_spec")
	public void insertU_discover_spec(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		String id = request.getParameter("id");
		String conn_id = request.getParameter("conn_id");
		String price_name = request.getParameter("price_name");
		String price = request.getParameter("price");
		String limit = request.getParameter("limit");
		String sort = request.getParameter("sort");
		String type = request.getParameter("type");
        
		Map<String,Object> map = new HashMap<String,Object>();
		
		try{
			if(id!=null && !"".equals(id.trim())){
				ApiU_discover_spec u_discover_spec = new ApiU_discover_spec();
				
    		    //u_discover_spec.setId(id);
    		    //u_discover_spec.setConn_id(conn_id);
    		    //u_discover_spec.setPrice_name(price_name);
    		    //u_discover_spec.setPrice(price);
    		    //u_discover_spec.setLimit(limit);
    		    //u_discover_spec.setSort(sort);
    		    //u_discover_spec.setType(type);
				
				apiU_discover_specService.insertU_discover_spec(u_discover_spec);
				// return
                map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "娣诲姞鎴愬姛锛�");
			}
		}catch(Exception e){
			logger.error(e);
            e.printStackTrace();  
    		map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
    		map.put(Utils.MESSAGE, "绯荤粺寮傚父锛�");
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 鏍规嵁id鏌ヨ
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/findU_discover_specById")
	public void findU_discover_specById(HttpServletRequest request, HttpServletResponse response) throws Exception {
	
		Map<String,Object> map = new HashMap<String,Object>();
		try{
		    String id = request.getParameter("id");
		    ApiU_discover_spec u_discover_spec = apiU_discover_specService.findU_discover_specById(Integer.valueOf(id));
		
			// return
            map.put("datas", u_discover_spec);
            map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "鏌ヨ鎴愬姛锛�");
		}catch(Exception e){
			logger.error(e);
            e.printStackTrace();  
    		map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
    		map.put(Utils.MESSAGE, "绯荤粺寮傚父锛�");
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));
        
	}
	
     /**
	 * 鏌ヨ鏁版嵁闆�
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/findU_discover_specs")
	public void findU_discover_specs(HttpServletRequest request, HttpServletResponse response) throws Exception {
	
		Map<String,Object> map = new HashMap<String,Object>();
		try{
		    List<ApiU_discover_spec> u_discover_specs = apiU_discover_specService.findU_discover_specs();
		
			// return
            map.put("datas", u_discover_specs);
            map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "鏌ヨ鎴愬姛锛�");
		}catch(Exception e){
			logger.error(e);
            e.printStackTrace();  
    		map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
    		map.put(Utils.MESSAGE, "绯荤粺寮傚父锛�");
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));
        
	}
	
    
	/**
	 * 淇敼
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception 
	 */
	@Transactional
	@RequestMapping(value = "updateU_discover_spec")
	public void updateU_discover_spec(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		String id = request.getParameter("id");
		String conn_id = request.getParameter("conn_id");
		String price_name = request.getParameter("price_name");
		String price = request.getParameter("price");
		String limit = request.getParameter("limit");
		String sort = request.getParameter("sort");
		String type = request.getParameter("type");
        
        
		//String id = request.getParameter("id");
		Map<String,Object> map = new HashMap<String,Object>();
		
		try{
			if(id!=null && !"".equals(id.trim())){
				ApiU_discover_spec u_discover_spec = new ApiU_discover_spec();
				//鑾峰彇鍘熸暟鎹�
				u_discover_spec  = apiU_discover_specService.findU_discover_specById(Integer.valueOf(id));
				//淇敼
				//u_discover_spec.setIs_open(Integer.valueOf(is_open));
				//
				int ret_i = apiU_discover_specService.updateU_discover_spec(u_discover_spec);
				
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "缂栬緫鎴愬姛锛�");
			}else{
				map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
				map.put(Utils.MESSAGE, "妫�鏌ュけ璐ワ紒");
			}
		}
		catch(Exception e){
			logger.error(e);
            e.printStackTrace();  
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "绯荤粺寮傚父锛�");
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}
    
	/**
	 * 鍒犻櫎
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value = "deleteU_discover_spec")
	public void deleteU_discover_spec(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
        String id = request.getParameter("id");
		Map<String,Object> map = new HashMap<String,Object>();
		
		try{
			if(id!=null && !"".equals(id.trim())){
	
				Integer ret_I = apiU_discover_specService.deleteU_discover_spec(Integer.parseInt(id));
				if(ret_I >0 ){
    				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
    				map.put(Utils.MESSAGE, "鍒犻櫎鎴愬姛锛�");
                }else{
				    map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
				    map.put(Utils.MESSAGE, "妫�鏌ュけ璐ワ紒");
			    }
			}
		}catch(Exception e){
			logger.error(e);
            e.printStackTrace();  
    		map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
    		map.put(Utils.MESSAGE, "绯荤粺寮傚父锛�");
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}
	
 
}


